package com.atguigu.atcrowdfunding.manager.controller;

import com.atguigu.atcrowdfunding.bean.Advertisement;
import com.atguigu.atcrowdfunding.bean.User;
import com.atguigu.atcrowdfunding.manager.service.AdvertService;
import com.atguigu.atcrowdfunding.util.AjaxResult;
import com.atguigu.atcrowdfunding.util.Const;
import com.atguigu.atcrowdfunding.util.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.HashMap;
import java.util.UUID;

/**
 * @author xzt
 * @create 2021-03-03 18:35
 */
@Controller
@RequestMapping("/advert")
public class AdvertController {

    @Autowired
    private AdvertService advertService;

    @RequestMapping("/index")
    public String index(){

        return "advert/index";
    }

    @ResponseBody
    @RequestMapping("/doIndex")
    public Object doIndex(@RequestParam("pageNo") Integer pageNo,@RequestParam("pageSize") Integer pageSize){

        System.out.println(pageNo);
        System.out.println(pageSize);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            HashMap<String, Integer> paramMap = new HashMap<>();
            paramMap.put("pageNo", pageNo);
            paramMap.put("pageSize", pageSize);

            Page page = advertService.queryPage(paramMap);

            ajaxResult.setSuccess(true);
            ajaxResult.setMessage("查询数据成功");
            ajaxResult.setPage(page);


        }catch (Exception e){
            ajaxResult.setSuccess(false);
            ajaxResult.setMessage("查询数据失败");
            e.printStackTrace();
        }

        return ajaxResult;
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "advert/add";
    }

    @ResponseBody
    @RequestMapping("/doAdd")
    public Object doAdd(Advertisement advertisement, HttpServletRequest request, HttpSession session){

        AjaxResult ajaxResult = new AjaxResult();

        try {
            MultipartHttpServletRequest mreq = (MultipartHttpServletRequest) request;

            MultipartFile mfile = mreq.getFile("advpic");
            String name = mfile.getOriginalFilename();
            String extname = name.substring(name.lastIndexOf("."));

            String iconpath = UUID.randomUUID().toString()+extname;

            ServletContext servletContext = session.getServletContext();
            String realPath = servletContext.getRealPath("/pics");

            String path = realPath + "\\adv\\" + iconpath;
            mfile.transferTo(new File(path));

            User user = (User) session.getAttribute(Const.LOGIN_USER);

            advertisement.setUserid(user.getId());
            advertisement.setIconpath(iconpath);
            advertisement.setStatus("1");

            int count = advertService.insertAdvert(advertisement);
            ajaxResult.setSuccess(true);

        }catch (Exception e){
            e.printStackTrace();
        }
        return ajaxResult;
    }

}
